using System;
using System.Windows.Forms;
using System.Collections.Generic;
using Microsoft.Services.TfsAlert.Core.Model;
using Microsoft.Services.TfsAlert.UIControl;

namespace Microsoft.Services.TfsAlert.Forms
{
    public partial class ApplicationSettings : Form
    {
        private const int COLUMN_WIDTH_OFFSET = 193;

        private const int COL_WIDTH_EVENT = 100;
        private const int COL_WIDTH_SELECTED = 50;
        private const int COL_WIDTH_FILTER = 600;

        private const string DIALOG_TITLE = "TfsAlert Application Settings";

        private Microsoft.Services.TfsAlert.UIControl.ApplicationSettingsController _settingsController;

        private DataGridViewTextBoxColumn _eventTypeDataGridViewTextBoxColumn;
        private DataGridViewCheckBoxColumn _selectedDataGridViewCheckBoxColumn;
        private DataGridViewTextBoxColumn _filterExpressionDataGridViewTextBoxColumn;

        public ApplicationSettings()
        {
            InitializeComponent();

            ConfigureControls();
        }

        #region Event Handlers

        private void filteringHelpButton_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();

            process.StartInfo.UseShellExecute = true;

            process.StartInfo.FileName = "http://blogs.msdn.com/csell/archive/2006/11/01/tfs-event-filtering.aspx";

            process.Start();
        }


        private void serverCombo_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            TfsServer server = (TfsServer) serverCombo.SelectedItem;

            FillInTextBoxes(server);

            if (server != null)
            {
                tfsEventBindingSource.DataSource = server.Events;
            }
        }

        private void addToolStripMenuItem_Click(object sender, System.EventArgs e)
        {

            _settingsController.AddEvent( (TfsServer)serverCombo.SelectedItem );
            TfsServer server = (TfsServer)serverCombo.SelectedItem;

            alertGrid.DataSource = null;
            alertGrid.DataSource = server.Events;
            //tfsEventBindingSource.DataSource = server.Events;
  
          
            //
        }

        private void ApplicationSettings_Resize(object sender, EventArgs e)
        {
            alertGrid.Columns["FilterExpression"].Width = alertGrid.Width - COLUMN_WIDTH_OFFSET;
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowIndex = 0;

            if (alertGrid.SelectedRows.Count == 0)
            {
                rowIndex = alertGrid.SelectedCells[0].RowIndex;

                alertGrid.Rows[rowIndex].Selected = true;
            }

            TfsServer server = (TfsServer) serverCombo.SelectedItem;
            TfsEvent tfsEvent = (TfsEvent) tfsEventBindingSource[rowIndex];

            _settingsController.DeleteEvent(server, tfsEvent);
            alertGrid.DataSource = null;
            alertGrid.DataSource = server.Events;
        }


        private void eventAddDeleteContext_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            eventAddDeleteContext.Items[1].Enabled = (alertGrid.Rows.Count > 0);
        }

        private void ApplicationSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            _settingsController.FormClosing();
        }

        private void ApplicationSettings_Load(object sender, EventArgs e)
        {
            _settingsController.FormLoad();

            IList<TfsServer> tfsList = null;

            _settingsController.SetDataSource(ref tfsList);

            serverCombo.DataSource = tfsList;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            _settingsController.SaveServers();

            _settingsController.FormClose();

            saveButton.Enabled = false;
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            _settingsController.DeleteServer((TfsServer)serverCombo.SelectedItem);

            RefreshServerList();

            if (serverCombo.Items.Count > 0)
            {
                serverCombo.SelectedIndex = 0;
            }
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            _settingsController.AddServer();

            RefreshServerList();
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            TfsServer server = (TfsServer)serverCombo.SelectedValue;

            _settingsController.EditServer(server);

            RefreshServerList();

            FillInTextBoxes(server);
        }

        private void userIdText_Leave(object sender, EventArgs e)
        {
            UpdateServer();
        }

        private void userIdText_TextChanged(object sender, EventArgs e)
        {
        }

        private void alertGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        #endregion

        #region Support Methods

        internal void Show(Microsoft.Services.TfsAlert.UIControl.ApplicationSettingsController settingsController)
        {
            _settingsController = settingsController;

            this.ShowDialog();
        }

        private void FillInTextBoxes(TfsServer server)
        {
            if (server != null)
            {
                uriText.Text = server.URI;
                userIdText.Text = server.UserName;
            }
            else
            {
                uriText.Text = string.Empty;
                userIdText.Text = string.Empty;
            }
        }
        /// <summary>
        /// This method is necessary to ge the list to update properly
        /// </summary>
        private void RefreshServerList()
        {
            Object hold = serverCombo.DataSource;
            serverCombo.DataSource = null;
            serverCombo.DataSource = hold;
        }

        private void UpdateServer()
        {
            TfsServer server = serverCombo.SelectedItem as TfsServer;

            server.UserName = userIdText.Text;
        }

         private void SetColumnOrder()
        {
            alertGrid.Columns.Clear();

            alertGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                _eventTypeDataGridViewTextBoxColumn,
                _selectedDataGridViewCheckBoxColumn,
                _filterExpressionDataGridViewTextBoxColumn});
        }

        private void ConfigureControls()
        {
            _filterExpressionDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
            _eventTypeDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
            _selectedDataGridViewCheckBoxColumn = new DataGridViewCheckBoxColumn();

            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();

            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
            dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
            dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            
            alertGrid.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;

            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;

            _eventTypeDataGridViewTextBoxColumn.DataPropertyName = "EventType";
            _eventTypeDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStyle2;
            _eventTypeDataGridViewTextBoxColumn.HeaderText = "Event Type";
            _eventTypeDataGridViewTextBoxColumn.Name = _eventTypeDataGridViewTextBoxColumn.DataPropertyName;
            _eventTypeDataGridViewTextBoxColumn.Width = COL_WIDTH_EVENT;

            _selectedDataGridViewCheckBoxColumn.DataPropertyName = "IsSelected";
            _selectedDataGridViewCheckBoxColumn.DefaultCellStyle = dataGridViewCellStyle2;
            _selectedDataGridViewCheckBoxColumn.HeaderText = "Selected";
            _selectedDataGridViewCheckBoxColumn.Name = _selectedDataGridViewCheckBoxColumn.DataPropertyName;
            _selectedDataGridViewCheckBoxColumn.Width = COL_WIDTH_SELECTED;

            _filterExpressionDataGridViewTextBoxColumn.DataPropertyName = "FilterExpression";
            _filterExpressionDataGridViewTextBoxColumn.HeaderText = "Filter Expression";
            _filterExpressionDataGridViewTextBoxColumn.Name = _filterExpressionDataGridViewTextBoxColumn.DataPropertyName;
            _filterExpressionDataGridViewTextBoxColumn.Width = COL_WIDTH_FILTER;

            SetColumnOrder();

            this.Text = DIALOG_TITLE;
        }

       #endregion

        private void tfsEventBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }


    }
}